package org.oproject.rmi.hessian.core.service;

import java.util.Arrays;


/**
 * 服务接口信息
 * @author SQ
 *
 */
public class ServiceInfo
{
	/**
	 * 服务名称
	 */
	private String name;
	
	/**
	 * 服务在JAVA层的实现类型
	 */
	private ServiceType serviceType;
	/*
	*//**
	 * 服务在JAVA层的实现类型编码
	 *//*
	private String serviceTypeCode;*/
	
	/**
	 * Service的cache支持类型
	 */
	private CacheType cacheType;
	/*
	*//**
	 * Service的cache支持类型的字符串编码
	 *//*
	private String cacheTypeCode;*/
	
	/**
	 * 是否是单例
	 */
	private boolean isSingle;
	
	/**
	 * 对象全名称即，org.oproject.rmi.hessian.core.service.ServiceInfo的形式
	 */
	private String classFullName;
	
	/**
	 * 服务执行的方法名称
	 */
	private String methodName;
		
	
	/**
	 * 调用该服务的参数类型
	 */
	@SuppressWarnings("unchecked")
	private Class[] parameterTypes;

	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("serviceName:");
		sb.append(name);
		sb.append("; serviceType:");
		sb.append(serviceType);
		sb.append("; isSingle:");
		sb.append(isSingle);
		sb.append("; classFullName:");
		sb.append(classFullName);
		sb.append("; methodName:");
		sb.append(methodName);
		if(null != parameterTypes && parameterTypes.length > 0)
		{
			sb.append(Arrays.asList(parameterTypes));
		}
		
		return sb.toString();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((classFullName == null) ? 0 : classFullName.hashCode());
		result = prime * result + (isSingle ? 1231 : 1237);
		result = prime * result
				+ ((methodName == null) ? 0 : methodName.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + Arrays.hashCode(parameterTypes);
		result = prime * result
				+ ((serviceType == null) ? 0 : serviceType.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ServiceInfo other = (ServiceInfo) obj;
		if (classFullName == null) {
			if (other.classFullName != null)
				return false;
		} else if (!classFullName.equals(other.classFullName))
			return false;
		if (isSingle != other.isSingle)
			return false;
		if (methodName == null) {
			if (other.methodName != null)
				return false;
		} else if (!methodName.equals(other.methodName))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (!Arrays.equals(parameterTypes, other.parameterTypes))
			return false;
		if (serviceType == null) {
			if (other.serviceType != null)
				return false;
		} else if (!serviceType.equals(other.serviceType))
			return false;
		if (serviceType.typeCode() == null) {
			if (other.serviceType.typeCode() != null)
				return false;
		} else if (!serviceType.typeCode().equals(other.serviceType.typeCode()))
			return false;
		return true;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public ServiceType getServiceType()
	{
		return serviceType;
	}

	public void setServiceType(ServiceType serviceType)
	{
		this.serviceType = serviceType;
	}

	public void setServiceType(String serviceTypeCode)
	{
		if(null != serviceTypeCode)
		{
			for(ServiceType _serviceType : ServiceType.values())
			{
				if(null != _serviceType.typeCode() && _serviceType.typeCode().equals(serviceTypeCode.trim()))
				{
					this.setServiceType(_serviceType);
					return;
				}
			}
		}
		
		throw new RuntimeException("setServiceTypeCode [" + serviceTypeCode +"] is not support...");
	}

	public CacheType getCacheType() 
	{
		return cacheType;
	}

	public void setCacheType(CacheType cacheType) 
	{
		this.cacheType = cacheType;
	}

	public void setCacheType(String cacheTypeCode) 
	{
		if(null != cacheTypeCode)
		{
			for(CacheType _cacheType : CacheType.values())
			{
				if(null != _cacheType.typeCode() && _cacheType.typeCode().equals(cacheTypeCode.trim()))
				{
					this.setCacheType(_cacheType);
					return ;
				}
			}
		}
		
		throw new RuntimeException("cacheTypeCode [" + cacheTypeCode + "] is not support...");
	}

	public boolean isSingle()
	{
		if(this.serviceType.equals(ServiceType.NORMAL))
		{
			return isSingle;
		}
		else
		{
			throw new RuntimeException("just normal java object support this method...");
		}
	}

	public void setSingle(boolean isSingle)
	{
		this.isSingle = isSingle;
	}
	
	public void setSingle(String isSingle)
	{
		if(null != isSingle && !isSingle.trim().equals(""))
		{
			if(isSingle.trim().equalsIgnoreCase("true"))
			{
				this.setSingle(true);
			}
			else
			{
				this.setSingle(false);
			}
		}
	}

	public String getClassFullName()
	{
		return classFullName;
	}

	public void setClassFullName(String classFullName)
	{
		this.classFullName = classFullName;
	}

	public String getMethodName()
	{
		return methodName;
	}

	public void setMethodName(String methodName)
	{
		this.methodName = methodName;
	}
	
	@SuppressWarnings("unchecked")
	public Class[] getParameterTypes() {
		return parameterTypes;
	}

	@SuppressWarnings("unchecked")
	public void setParameterTypes(Class[] parameterTypes) {
		this.parameterTypes = parameterTypes;
	}

	/**
	 * 验证服务信息是否完整、符合规则
	 * @return
	 */
	public boolean validate()
	{
		if(null == this.name 
				|| this.name.trim().equals(""))
		{
			return false;
		}
		
		if(null == this.methodName 
				|| this.methodName.trim().equals(""))
		{
			return false;
		}
		
		if(null == this.serviceType)
		{
			return false;
		}
				
		//普通类型的service必须注册类的全路径名
		if(this.serviceType.typeCode().equals(ServiceType.NORMAL.typeCode())
				&& (null == this.classFullName || this.classFullName.trim().equals("")))
		{
			return false;
		}
		
		return true;
	}
}
